package com.cyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.HealthDao;
import com.cyz.dao.OldDao;
import com.cyz.entity.Health;
import com.cyz.entity.Old;
import com.cyz.vo.HealthDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/doctor/health")
public class HealthController {

    @Autowired
    private HealthDao healthDao;

    @Autowired
    private OldDao oldDao;

    @PostMapping("/query")
    private List<HealthDTO> query(){
        List<Health> health = healthDao.selectList(new QueryWrapper<>());
        List<HealthDTO> collect = health.stream().map(item -> {
            HealthDTO healthDTO = new HealthDTO();
            Old old = oldDao.selectById(item.getId());
            if (old != null){
                BeanUtils.copyProperties(item, healthDTO);
                healthDTO.setHealth(old.getHealth());
            }
            return healthDTO;
        }).collect(Collectors.toList());

        return collect;
    }

    @PostMapping("/queryPro")
    private List<HealthDTO> queryPro(){
        QueryWrapper<Health> wrapper = new QueryWrapper<>();
        wrapper.lt("sugar", 3.9).or().gt("sugar",6.1)
                .or().lt("pressure", 60).or().gt("pressure",139)
                .or().lt("heartbeat", 60).or().gt("heartbeat",100);

        List<Health> health = healthDao.selectList(wrapper);
        List<HealthDTO> collect = health.stream().map(item -> {
            HealthDTO healthDTO = new HealthDTO();
            Old old = oldDao.selectById(item.getId());
            if (old != null){
                BeanUtils.copyProperties(item, healthDTO);
                healthDTO.setHealth(old.getHealth());
            }
            return healthDTO;
        }).collect(Collectors.toList());

        return collect;
    }

    @PostMapping("/upload")
    private String upload(HealthDTO healthDTO){
        Health health = new Health();
        BeanUtils.copyProperties(healthDTO, health);
        int insert = healthDao.insert(health);
        if (insert != 0){
            Old old = oldDao.selectById(healthDTO.getId());
            old.setHealth(healthDTO.getHealth());
            oldDao.update(old, new QueryWrapper<Old>().eq("id", healthDTO.getId()));
        }
        return "添加成功";
    }

    @PostMapping("/query/{id}")
    private HealthDTO query(@PathVariable int id){
        HealthDTO healthDTO = new HealthDTO();
        Health health = healthDao.selectById(id);
        BeanUtils.copyProperties(health, healthDTO);
        Old old = oldDao.selectById(id);
        healthDTO.setHealth(old.getHealth());
        return healthDTO;
    }

    @PostMapping("update")
    private String update(HealthDTO healthDTO){
        Health health = new Health();
        BeanUtils.copyProperties(healthDTO, health);
        int id = healthDTO.getId();

        healthDao.update(health, new QueryWrapper<Health>().eq("id", id));

        Old old = oldDao.selectById(id);
        oldDao.update(old, new QueryWrapper<Old>().eq("id",id));
        return "更新成功";
    }
}
